package com.yiqi.zhiyuan.feature.order.view.adapter;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.suozhang.framework.utils.DateUtil;
import com.suozhang.framework.utils.DensityUtil;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.vo.OrderVO;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 立即前往弹框
 *
 * @author LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/25 15:22
 */
public class CancleOrderDialog extends Dialog {


    @BindView(R.id.tv_order_cancle_departure)
    TextView mTvOrderCancleDeparture;
    @BindView(R.id.tv_order_cancle_destination)
    TextView mTvOrderCancleDestination;
    @BindView(R.id.tv_order_cancle_time)
    TextView mTvOrderCancleTime;
    @BindView(R.id.tv_order_cancle_reason)
    TextView mTvOrderCancleReason;
    @BindView(R.id.tv_order_point_1)
    TextView mTvOrderPoint1;
    @BindView(R.id.tv_order_point_2)
    TextView mTvOrderPoint2;
    private Context context;

    private MyClickListenerInterface listenerInterface;
    private LayoutInflater layoutInflater;
    OrderVO orderVO;
    boolean isCancle = false;

    public CancleOrderDialog(Context context, OrderVO orderVO, boolean isCancle) {
        super(context, R.style.ActionImmediatelyDialogStyle);
        this.context = context;
        this.isCancle = isCancle;
        this.orderVO = orderVO;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        View view = layoutInflater.inflate(R.layout.dialog_order_cancle, null);
        ButterKnife.bind(this, view);
        setContentView(view);


        mTvOrderCancleDeparture.setText(orderVO.getDeparture());
        mTvOrderCancleDestination.setText(orderVO.getDestination());
        mTvOrderCancleTime.setText(TextUtils.isEmpty(orderVO.getUpdateTime()) ? "" : isCancle ? DateUtil.stampToDate(Long.parseLong(orderVO.getUpdateTime())) : orderVO.getUpdateTime());
        mTvOrderCancleReason.setText(String.format("取消原因:%s\t%s", TextUtils.isEmpty(orderVO.getCancelTypeContent()) ? "" : orderVO.getCancelTypeContent(), TextUtils.isEmpty(orderVO.getCancelReason()) ? "" : orderVO.getCancelReason()));


        mTvOrderPoint1.setText("途径地点\t\t" + orderVO.getWaypoint1());
        mTvOrderPoint2.setText("途径地点\t\t" + orderVO.getWaypoint2());

        mTvOrderPoint1.setVisibility(TextUtils.isEmpty(orderVO.getWaypoint1()) ? View.GONE : View.VISIBLE);
        mTvOrderPoint2.setVisibility(TextUtils.isEmpty(orderVO.getWaypoint2()) ? View.GONE : View.VISIBLE);

        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        setCanceledOnTouchOutside(false);
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(DensityUtil.dp2px(33), 0, DensityUtil.dp2px(33), 0);
        getWindow().setAttributes(layoutParams);


    }

    public void setListener(MyClickListenerInterface listener) {
        this.listenerInterface = listener;
    }


    @OnClick(R.id.btnv_order_cancle)
    public void onViewClicked() {
        if (listenerInterface != null) {
            dismiss();
            listenerInterface.doConfirm();
        }
    }


    public interface MyClickListenerInterface {
        void doConfirm();
    }
}
